package infod

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/common"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/log"
)

// WalkInfos 根据条件遍倒序历符合条件的所有信息
func WalkInfos(ctx ddd.Context, iar InfoAggregateRoot, is InfoSearch, total int, handler func(i *InfoAggregate)) error {
	pi := common.PageInfo{
		Mode:     1,
		PageSize: 100,
		Desc:     true,
		Direct:   0,
	}
	for n := 0; total == 0 || (total > 0 && n < total); n++ {

		pr, infos, err := iar.QueryPage(ctx, is, pi)
		if err != nil {
			log.Warnf("读取信息列表失败：查询条件：%v 分页信息 %v 错误:%v", is, pi, err)
			return err
		}
		for _, i := range infos {
			handler(&i)
		}

		if pr.End == 1 || len(infos) == 0 {
			break
		}
	}
	return nil
}
